<template>
	<view style="padding: 20rpx;">
		<view class="box">
			<uni-forms :modelValue="form" :rules="rules" ref="formRef" label-width="180rpx" label-align="right">
				<uni-forms-item label="地址" name="address" required>
					<uni-easyinput type="textarea" v-model="form.address" placeholder="请输入地址" />
				</uni-forms-item>
				<uni-forms-item label="联系人" name="user" required>
					<uni-easyinput type="text" v-model="form.user" placeholder="请输入联系人" />
				</uni-forms-item>
				<uni-forms-item label="联系电话" name="phone" required>
					<uni-easyinput type="text" v-model="form.phone" placeholder="请输入联系电话" />
				</uni-forms-item>
				
				<uni-forms-item>
					<button type="primary" size="mini" @click="saveAddress">保 存</button>
				</uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: uni.getStorageSync('xm-user'),
				form: {},
				rules: {
					address: {
						rules:[
							{
								required: true,
								errorMessage: '请输入地址'
							}
						]
					},
						
					user: {
						rules:[
							{
								required: true,
								errorMessage: '请输入联系人'
							}
						]
					},
					
					phone: {
						rules:[
							{
								required: true,
								errorMessage: '请输入联系电话'
							}
						]
					},
				}
			}
		},
		onLoad(option){
			let addressId = option.addressId
			
			if(addressId > 0){
				this.$request.get('/address/selectById/'+addressId).then(res => {
					this.form = res.data || {}
				})
			}
			
		},
		methods: {
			saveAddress(){
				//验证
				this.$refs.formRef.validate().then(res => {
					this.$request.request({
						method: this.form.id ? 'PUT' : 'POST',
						url: this.form.id ? '/address/update' : '/address/add',
						data: this.form
					}).then(res => {
						if(res.code === '200'){
							uni.showToast({
								icon: 'success',
								title:'新增地址成功'
							})
							uni.navigateBack()
						} else {
							uni.showToast({
								icon: 'error',
								title: res.msg
							})
						}
					})
				}).catch(err => {
					console.log('err',err)
				})
			}
		}
	}
</script>

<style>

</style>
